/**
# Example to use srs-librtmp
# see: https://github.com/winlinvip/simple-rtmp-server/wiki/v2_CN_SrsLibrtmp
    gcc main.cpp srs_librtmp.cpp -g -O0 -lstdc++ -o output
*/
#include <windows.h>
#include <stdio.h>
#include "..\src\srs\srs_librtmp.h"

#include "myThread.h"

void function(int thread_count)
{
	char rtmp_url[256] = { 0 };
	sprintf_s(rtmp_url, "rtmp://192.168.2.77:1935/live/livestream%d", thread_count);
	srs_rtmp_t rtmp = srs_rtmp_create(rtmp_url);

	if (srs_rtmp_handshake(rtmp) != 0) {
		srs_human_trace("simple handshake failed.");
		goto rtmp_destroy;
	}
	srs_human_trace("simple handshake success");

	if (srs_rtmp_connect_app(rtmp) != 0) {
		srs_human_trace("connect vhost/app failed.");
		goto rtmp_destroy;
	}
	srs_human_trace("connect vhost/app success");

	if (srs_rtmp_play_stream(rtmp) != 0) {
		srs_human_trace("play stream failed.");
		goto rtmp_destroy;
	}
	srs_human_trace("play stream success");

	for (;;) {
		int size;
		char type;
		char* data;
		u_int32_t timestamp, pts;

		if (srs_rtmp_read_packet(rtmp, &type, &timestamp, &data, &size) != 0) {
			goto rtmp_destroy;
		}
		if (srs_utils_parse_timestamp(timestamp, type, data, size, &pts) != 0) {
			goto rtmp_destroy;
		}
		srs_human_trace("got packet: type=%s, dts=%d, pts=%d, size=%d",
			srs_human_flv_tag_type2string(type), timestamp, pts, size);

		free(data);
	}
rtmp_destroy:
	srs_rtmp_destroy(rtmp);
}

myThread* g_thread[50] = { 0 };

int main(int argc, char** argv)
{
    // startup socket for windows.
    WSADATA WSAData;
    if (WSAStartup(MAKEWORD(1, 1), &WSAData)) {
        printf("WSAStartup failed.\n");
        return -1;
    }
	for (size_t i = 0; i < 50; i++)
	{
		g_thread[i] = new myThread();
		g_thread[i]->Start(function, i);
	}
	getchar();
    
rtmp_destroy:
    // cleanup socket for windows.
    WSACleanup();
    return 0;
}

